package com.jy.wms.common.base;

import com.github.pagehelper.Page;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

/**
 * @Author:zzp
 * @Description: Created by Administrator on 2017/8/13.
 * @Date:Create in 20:51 2017/8/13
 */
public class BaseController<T,M extends Serializable> {
    /**
     * 日志对象
     */
    protected Logger logger = (Logger) LoggerFactory.getLogger(getClass());
    private static final JSONObject jsonObject = new JSONObject();
    public static JSONObject getJSONObject(){
        return jsonObject;
    }
    private static final JSONArray jsonArray = new JSONArray();
    public static JSONArray getJSONArray(){
        return jsonArray;
    }
    @Autowired
    protected T baseService;
    /**
     * 渲染失败数据
     *
     * @return result
     */
    protected JsonResult renderError() {
        JsonResult result = new JsonResult();
        result.setSuccess(false);
        result.setStatus("500");
        return result;
    }

    /**
     * 渲染失败数据（带消息）
     *
     * @param msg 需要返回的消息
     * @return result
     */
    protected JsonResult renderError(String msg) {
        JsonResult result = renderError();
        result.setMsg(msg);
        return result;
    }

    /**
     * 渲染成功数据
     *
     * @return result
     */
    protected JsonResult renderSuccess() {
        JsonResult result = new JsonResult();
        result.setSuccess(true);
        result.setStatus("200");
        return result;
    }

    /**
     * 渲染成功数据（带信息）
     *
     * @param msg 需要返回的信息
     * @return result
     */
    protected JsonResult renderSuccess(String msg) {
        JsonResult result = renderSuccess();
        result.setMsg(msg);
        return result;
    }

    /**
     * 渲染成功数据（带数据）
     *
     * @param obj 需要返回的对象
     * @return result
     */
    protected JsonResult renderSuccess(Object obj) {
        JsonResult result = renderSuccess();
        result.setObj(obj);
        return result;
    }
    protected  ResultPage<M> renderPage(Page<M> list){
        ResultPage<M> page=new ResultPage<M>();
        page.setContentList(list);
        page.setTotals((int) list.getTotal());
        return page;
    }

    /**
     * 添加Model消息
     * @param message
     */
   /* protected void addMessage(Map<String,String>... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages){
            sb.append(message).append(messages.length>1?"<br/>":"");
        }
    }*/
}
